package cn.sinata.wanglin.guide;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.WindowManager;

import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DensityUtil;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.mine.activity.LoginStyleActivity;

/**
 * 引导页面
 */
public class GuideActivity extends BaseActivity {
	ViewPager viewPager;
	private int flaggingWidth;// 滑动关闭引导页所需滚动的长度
	private GestureDetector gestureDetector; // 用户滑动
	private int currentItem = 0; // 当前图片的位置
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
				//初始化viewPager
		 gestureDetector = new GestureDetector(new GuideViewTouch());
		 flaggingWidth = DensityUtil.dip2px(this,20);
	     setViewPager();
	}
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if (gestureDetector.onTouchEvent(event)) {
			event.setAction(MotionEvent.ACTION_CANCEL);
		}
		return super.dispatchTouchEvent(event);
	}
	@SuppressWarnings("deprecation")
	private void setViewPager() {

		viewPager = (ViewPager) findViewById(R.id.viewPager);
		//1创建dapter
		FragmentPagerAdapter adapter=new adapter(getSupportFragmentManager());
		//2绑定dapter
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				currentItem = position;
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});

		
	}
	/**
	 * 当滑动到最后一页时，继续滑动将进入到联赛选择列表页
	 */
	private class GuideViewTouch extends
			GestureDetector.SimpleOnGestureListener {
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2,
							   float velocityX,float velocityY) {
			if (currentItem == 2) {
				if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
						- e2.getY()) && (e1.getX() - e2.getX() <= (-flaggingWidth)
						|| e1.getX() - e2.getX() >= flaggingWidth)) {
					if (e1.getX() - e2.getX() >= flaggingWidth) {
						ActivityUtil.create(mContext).go(LoginStyleActivity.class)
								.start();
						finish();
						return true;
					}
				}
			}
			return false;
		}
	}
	public class adapter extends FragmentPagerAdapter{

		public adapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position){
				case 0:
					return GuideOneFragment.newInstance(position);

				case 1:
					return	GuideTwoFragment.newInstance(position);

				case 2:
					return GuideThreeFragment.newInstance(position);

			}
			return new Fragment();
		}

		@Override
		public int getCount() {
			
			return 3;
		}
		
	}
}
